<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction :actions="[
            {
              tooltip: '修改',
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record.id),
            },
            {
              tooltip: '删除',
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record.id),
                placement: 'left'
              },
            },
          ]" />
        </template>
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import { useRouter } from 'vue-router'

import { TableSchema, searchFormSchema } from './data';
import { list, del } from '/@/api/project/reason';

export default defineComponent({
  components: { BasicTable, TableAction },
  setup() {
    const router = useRouter();

    const [registerTable, { reload }] = useTable({
      api: list,
      columns: TableSchema,
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
        showAdvancedButton: false,
        showResetButton: false,
        autoSubmitOnEnter: true,
      },
      canColDrag: true,
      useSearchForm: true,
      bordered: true,
      showIndexColumn: false,
      actionColumn: {
        width: 160,
        title: '操作',
        dataIndex: 'action',
        fixed: false,
      },
    });

    function handleCreate() {
      router.push('/project/reason/view/0');
    }

    function handleEdit(id) {
      router.push('/project/reason/view/' + id);
    }

    function handleDelete(id) {
      del(id).then(() => {
        reload();
      });
    }

    return {
      registerTable,
      handleCreate,
      handleEdit,
      handleDelete,
    };
  },
});
</script>
